import pandas as pd
import matplotlib.pyplot as plt
import datetime

# Fetching the stock price data
nvidia_symbol = "NVDA"
tesla_symbol = "TSLA"
api_key = "JA59FR8UE0X3UBND"  # Replace with your own API key

# Define the date range for YTD (January 1st till today)
today = datetime.date.today().strftime("%Y-%m-%d")
year_start = datetime.date(datetime.date.today().year, 1, 1).strftime("%Y-%m-%d")

# Fetching stock price data for NVIDIA
nvidia_url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol={nvidia_symbol}&outputsize=full&apikey={api_key}"
nvidia_data = pd.read_csv(nvidia_url)
nvidia_data = nvidia_data[(nvidia_data["timestamp"] >= year_start) & (nvidia_data["timestamp"] <= today)]
nvidia_data = nvidia_data.sort_values("timestamp")

# Fetching stock price data for Tesla
tesla_url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol={tesla_symbol}&outputsize=full&apikey={api_key}"
tesla_data = pd.read_csv(tesla_url)
tesla_data = tesla_data[(tesla_data["timestamp"] >= year_start) & (tesla_data["timestamp"] <= today)]
tesla_data = tesla_data.sort_values("timestamp")

# Plotting the chart
fig, ax = plt.subplots()
ax.plot(nvidia_data["timestamp"], nvidia_data["close"], label="NVIDIA")
ax.plot(tesla_data["timestamp"], tesla_data["close"], label="Tesla")

# Formatting the chart
plt.title("YTD Stock Price Changes - NVIDIA vs Tesla")
plt.xlabel("Date")
plt.ylabel("Stock Price (USD)")
plt.xticks(rotation=45)
plt.legend()

# Display the chart
plt.show()